
//
//  ThemeButton.m
//  weibo-01
//
//  Created by HZApple on 16/5/10.
//  Copyright © 2016年 hdu. All rights reserved.
//

#import "ThemeButton.h"
#import "ThemeManager.h"

@implementation ThemeButton

- (instancetype)initWithFrame:(CGRect)frame {
    if (self = [super initWithFrame:frame]) {
        
        
        [self _observeThemeChangeNotification];
    }
    return self;
}
- (void)awakeFromNib {
    
    [super awakeFromNib];

    [self _observeThemeChangeNotification];
}

- (void)_observeThemeChangeNotification {
    
    
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(themeChangeAction:) name:kThemeChangeNotification object:nil];
   
}

- (void)themeChangeAction:(NSNotification *)notification {
    
    [self loadImage];
    
}

- (void)setNormalImgName:(NSString *)normalImgName {
    
    if (_normalImgName != normalImgName) {
        _normalImgName = [normalImgName copy];
        
        [self loadImage];
    }
    
}

- (void)setHighlightImgName:(NSString *)highlightImgName {
    
    if (_highlightImgName != highlightImgName) {
        _highlightImgName = [highlightImgName copy];
        
        [self loadImage];
    }
}


- (void)loadImage {
    
    ThemeManager *manager = [ThemeManager sharedManager];
    
    UIImage *normalImg = [manager themeImageWithImageName:self.normalImgName];
    
    UIImage *hilightImg = [manager themeImageWithImageName:self.highlightImgName];
    
    [self setImage:normalImg forState:UIControlStateNormal];
    
    [self setImage:hilightImg forState:UIControlStateHighlighted];
}

- (void)dealloc {
    [[NSNotificationCenter defaultCenter] removeObserver:self name:kThemeChangeNotification object:nil];
}

@end
